package org.lys.poi;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.TranscodingHints;
import org.apache.batik.transcoder.image.ImageTranscoder;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.keys.DOMImplementationKey;
import org.apache.batik.transcoder.wmf.tosvg.WMFTranscoder;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.Range;

public class Word {
	/**
	 * @Description: 读取word并替换word中的文字
	 * @Create: 2012-12-18 上午11:14:50
	 * @author lys
	 * @update logs
	 * @param path
	 * @param map
	 */
	public static void writeDoc(String path, Map<String, String> map) {
		try {
			FileInputStream in = new FileInputStream(new File(
					"I:\\template.doc"));
			HWPFDocument hdt = new HWPFDocument(in);
			Range range = hdt.getRange();
			// 读取word文本内容
			System.out.println(range.text());
			// //替换文本内容
			// for (Map.Entry<String,String> entry:map.entrySet()) {
			// range.replaceText(entry.getKey(),entry.getValue());
			// }
			//
			// ByteArrayOutputStream ostream = new ByteArrayOutputStream();
			// FileOutputStream out = new FileOutputStream("g:\\处理后的ksap.doc");
			// hdt.write(ostream);
			//
			// //输出字节流
			// out.write(ostream.toByteArray());
			// ostream.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Description: 取得文档中的文本
	 * @Created Time: 2013-4-12 下午2:04:36
	 * @Author lys
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static String getBasicText(String fileName) throws Exception {
		WordExtractor extractor = new WordExtractor(Word.class.getClassLoader()
				.getResourceAsStream(fileName));
		return extractor.getText();
	}

	/**
	 * @Description: 抽取段落文本
	 * @Created Time: 2013-4-12 下午2:34:38
	 * @Author lys
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static String[] getBasicParagraphText(String fileName)
			throws Exception {
		WordExtractor extractor = new WordExtractor(Word.class.getClassLoader()
				.getResourceAsStream(fileName));
		return extractor.getParagraphText();
	}

	/**
	 * @Description: 以Range方式读取Text
	 * @Created Time: 2013-4-12 下午2:38:58
	 * @Author lys
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static String getRangeText(String fileName) throws Exception {
		HWPFDocument hdt = new HWPFDocument(Word.class.getClassLoader()
				.getResourceAsStream(fileName));
		Range range = hdt.getRange();
		return range.text();
	}

	/**
	 * @Description: 以Range形式抽取ParagraphText
	 * @Created Time: 2013-4-12 下午2:49:00
	 * @Author lys
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static String[] getRangeParagraphText(String fileName)
			throws Exception {
		HWPFDocument hdt = new HWPFDocument(Word.class.getClassLoader()
				.getResourceAsStream(fileName));
		Range range = hdt.getRange();
		int numParagraphs = range.numParagraphs();
		String[] paragraphArray = new String[numParagraphs];
		for (int i = 0; i < numParagraphs; i++) {
			paragraphArray[i] = range.getParagraph(i).text();
		}
		return paragraphArray;
	}
	
	public static void wmfToJpg(String fileName) throws Exception {
		InputStream wmfStream = new FileInputStream(new File(fileName));
		ByteArrayOutputStream imageOut = new ByteArrayOutputStream();
		int noOfByteRead = 0;
		while ((noOfByteRead = wmfStream.read())!= -1) {
			imageOut.write(noOfByteRead);
		}
		imageOut.flush();
		
		WMFTranscoder transcoder = new WMFTranscoder();
		TranscodingHints hints = new TranscodingHints();
		hints.put(ImageTranscoder.KEY_WIDTH, 20f);
		hints.put(ImageTranscoder.KEY_HEIGHT, 20f);
		transcoder.setTranscodingHints(hints);
		
		TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(
				imageOut.toByteArray()));
		
		ByteArrayOutputStream svg = new ByteArrayOutputStream();
		TranscoderOutput output = new TranscoderOutput(svg);

		transcoder.transcode(input, output);
		
		
		String svgFile = StringUtils.replace(fileName, "wmf", "svg");
		FileOutputStream fileOut = new FileOutputStream(svgFile);
		fileOut.write(svg.toByteArray());
		fileOut.flush();
		//完成了wmf转化成svg
		JPEGTranscoder it = new JPEGTranscoder(); 
		ByteArrayOutputStream jpg = new ByteArrayOutputStream();
		
		it.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,new Float(0.8));
        
		it.transcode(
				new TranscoderInput(new ByteArrayInputStream(svg.toByteArray())),
				new TranscoderOutput(jpg));
		String jpgFile = StringUtils.replace(fileName, "wmf", "jpg");
		FileOutputStream jpgOut = new FileOutputStream(jpgFile);
		jpgOut.write(jpg.toByteArray());
		jpgOut.flush();
	}

}
